//
//  DatePickerView.swift
//  SmartHousekeeper
//
//  Created by 张磊 on 2020/2/22.
//  Copyright © 2020 LS. All rights reserved.
//

import UIKit




class DatePickerView: UIView {
    typealias datePickerViewBlcok = (_ date:String, _  type:datePickerViewType) -> ()
    var datePickerBlcok : datePickerViewBlcok?  = nil
    enum datePickerViewType {
        case date
        case time
    }
    var dateType : datePickerViewType? {
        didSet {
            if dateType == .date {
                datePickerView.datePickerMode = UIDatePicker.Mode.date;
                datePickerView.minimumDate = NSDate() as Date;
                
            } else {
                datePickerView.calendar = NSCalendar.current
                datePickerView.calendar = Calendar.current;
                
                datePickerView.locale = Locale(identifier: "en_GB");
                
                datePickerView.timeZone = TimeZone.current;
                datePickerView.datePickerMode = UIDatePicker.Mode.time
            }
        }
    }
    
    lazy var topView : UIView = {
        let value = UIView(frame: CGRect(x: 0, y: ScreenHeight, width: ScreenHeight, height: 35))
        value.backgroundColor = RGBColor(r: 240, g: 240, b: 240, a: 1)
        return value
    }()
    
    lazy var finshBtn : UIButton = {
        let value = UIButton(textColor: RGBColor(r: 62, g: 134, b: 230, a: 1), textFont: UIFont.boldSystemFont(ofSize: Adapter(15)))
        value.setTitle("确定", for: .normal)
        value.frame = CGRect(x: ScreenWidth - 50, y: 0, width: 50, height: 35)
        value.layer.cornerRadius = Adapter(7)
        value.layer.masksToBounds = true
        return value
    }()
    lazy var cancleBtn : UIButton = {
        let value = UIButton(textColor: RGBColor(r: 62, g: 134, b: 230, a: 1), textFont: UIFont.boldSystemFont(ofSize: Adapter(15)))
        value.frame = CGRect(x: 0, y: 0, width: 50, height: 35)
        value.setTitle("取消", for: .normal)
        value.layer.cornerRadius = Adapter(7)
        value.layer.masksToBounds = true
        return value
    }()
    
    var datePickerView : UIDatePicker = {
        let top : CGFloat = UIDevice.isX() ? 88.0 : 64.0
        let datePick = UIDatePicker(frame: CGRect(x: 0, y: ScreenHeight, width: ScreenWidth, height: Adapter(265)));
        //设置显示时间样式，中or英，默认英文显示样式
        datePick.locale = NSLocale(localeIdentifier: "zh_CN") as Locale;//中文
        datePick.backgroundColor = RGBColor(r: 240, g: 240, b: 240, a: 1);
        //设置默认的日期
        datePick.date = NSDate() as Date;
        
        //        //设置可以选择的最大日期
        //        datePick.maximumDate = NSDate(timeIntervalSinceNow: 60*60*24*7) as Date;
        
        return datePick
    }()
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    private func setUI(){
        
        
        self.addSubview(topView)
        
        
        topView.addSubview(finshBtn)
        
        
        topView.addSubview(cancleBtn)
        
        
        self.addSubview(datePickerView)
        
        
        
        
        //添加方法获取显示的日期值
        datePickerView.addTarget(self, action: #selector(changeValueDate(datePickerV:)), for: .valueChanged)
        
        finshBtn.sce.add(.touchUpInside) { (btn) in
            
            
            switch self.dateType {
            case .time:
                let timeS = timeStringForDate(self.datePickerView.date, "HH-mm")
                self.datePickerBlcok?(timeS,.time)
                break
            case .date:
                let timeS = timeStringForDate(self.datePickerView.date, "yyyy-MM-dd")
                self.datePickerBlcok?(timeS,.date)
                break
                
            default:
                break
            }
            
            
            self.dismissPicker()
            
            
            
        }
        cancleBtn.sce.add(.touchUpInside) { (btn) in
            self.dismissPicker()
        }
    }
    func showPicker(type:datePickerViewType) {
        dateType = type
        
        
        
        
        
        UIView.animate(withDuration: 0.5, animations: {
            self.topView.frame =  CGRect(x: 0, y: ScreenHeight - Adapter(300), width: ScreenHeight, height: Adapter(35))
            self.datePickerView.frame =  CGRect(x: 0, y: (ScreenHeight - Adapter(265)), width: ScreenWidth, height: Adapter(265));
        }) { (isfinsh) in
            if isfinsh {
                
            }
            
        }
    }
    func dismissPicker() {
        
        
        
        UIView.animate(withDuration: 0.5, animations: {
            self.topView.frame =  CGRect(x: 0, y: ScreenHeight, width: ScreenHeight, height: Adapter(35))
            self.datePickerView.frame =  CGRect(x: 0, y: (ScreenHeight + Adapter(35)), width: ScreenWidth, height: Adapter(265));            })
        { (isfinsh) in
            self.removeFromSuperview()
        }
    }
    
    @objc func changeValueDate(datePickerV:UIDatePicker) {
        
    }
    
    
}
